<?php
namespace Admin\Model;
use Think\Model;

class GoodsModel extends Model{
	//自动验证
	protected  $_validate=array(
			array('goods_name','3,10','商品名长度不对',self::EXISTS_VALIDATE,'length',self::MODEL_INSERT),
			array('goods_sn','','商品号已存在',self::EXISTS_VALIDATE,'unique',self::MODEL_INSERT),
			array('shop_price','check_price','价格不对',self::EXISTS_VALIDATE,'callback',self::MODEL_INSERT),
	);
	
	//自动过滤,不在规则里面的字段，新增时它的值不会被添加到数据库
	//protected $insertFields='goods_name,goods_sn,shop_price';
	
	//自动过滤,不在规则里面的字段，修改时时它的值不会被添加到数据库
	protected $updateFields=array('goods_name','goods_sn');
	
	//自动填充
	protected $_auto=array(
			array('add_time','time',self::MODEL_INSERT,'function'),
	);
	
    //验证规则的回调函数
	protected function check_price($field){
		if($field==0){
			return false;
		}
	}
	
	
	
	public function goodsAdd($arr){
		//可以在create创建数据之前用field指定写到数据库的字段
		$this->field('goods_name','goods_sn','shop_price')->create($arr);
		if($this->create($arr)){
			return $this->add();
		}else{
			echo $this->getError();
			exit();
		}
	}
	
	public function getGoods(){
		return $this->field('goods_id','goods_sn','goods_name','shop_price')->select();
	}
}
